﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SGLisp;
namespace GameUnit
{
    public static class UnitManager
    {
        public static Hashtable UnitsTable;
        public static Hashtable AbilitiesTable;
        public static Dictionary<long, Unit> Units = new Dictionary<long, Unit>();
        public static void Load(string filePath)
        {
            UnitsTable = (Hashtable)SGLisp.Helper.EvalValueFromFile(filePath + "/Units.clj", RT.MainContext);
            AbilitiesTable = (Hashtable)SGLisp.Helper.EvalValueFromFile(filePath + "/Abilities.clj", RT.MainContext);
            Debug.Log("load units ability table");
        }

        public static Hashtable GetUnitConfig(string unitKey)
        {
            return (Hashtable)UnitsTable[unitKey];
        }
        public static Unit CreateUnit(string unitName, long uuid, Transform parent)
        {
            Hashtable unitTable = (Hashtable)UnitsTable[unitName];
            GameObject newUnitG = new GameObject(unitName);
            Unit newUnit = (Unit)newUnitG.AddComponent((string)unitTable[":type"]);
            newUnit.Init(unitTable);
            newUnit.UnitId = uuid;
            Units[uuid] = newUnit;
            UnityHelpler.AddChild(newUnitG.transform, parent);
            return newUnit;
        }

        public static Unit GetUnit(long uuid)
        {
            return Units[uuid];
        }
    }
}